import 'package:flutter/material.dart';
import 'package:fsharing/core/viewmodels/pages/mine/manage/bank_type_model.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';

class BankTypePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => BankTypeModel(Provider.of(context, listen: false)),
      child: BackgroundContainer(
        child: Scaffold(
          appBar: AppBar(
            leading: RoundBackButton(),
            title: Text('账户类型'),
          ),
          body: Consumer<BankTypeModel>(
            builder: (context, model, child) {
              switch (model.viewState) {
                case ViewState.busy:
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                case ViewState.error:
                  return Center(
                    child: Text(model.errorMessage),
                  );
                case ViewState.success:
                  return _ListWidget(
                    list: model.map.entries.toList(),
                  );
                default:
                  return Container();
              }
            },
          ),
        ),
      ),
    );
  }
}

class _ListWidget extends StatelessWidget {
  final List<MapEntry<String, dynamic>> list;

  const _ListWidget({
    Key key,
    @required this.list,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (context, index) {
        var mapEntry = list[index];
        return ListTile(
          title: Text(mapEntry.value),
          trailing: Icon(Icons.keyboard_arrow_right),
          onTap: () => _onItemClicked(context, mapEntry),
        );
      },
      itemCount: list.length,
    );
  }

  void _onItemClicked(BuildContext context, MapEntry<String, dynamic> mapEntry) {
    Navigator.of(context).pop(mapEntry);
  }
}
